//
//  HWMineTopCell.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/28.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWMineTopCell: UICollectionViewCell {
    /// 头像
    var imageView:UIImageView!
    /// 名字
    var nameLabel:UILabel!
    /// 可用余额
    var balanceTitle:UILabel!
    var balanceLabel:UILabel!
    /// 提现
    var withdrawalButton:UIButton!
    var lineView:UIView!
    /// 充值
    var rechargeButton:UIButton!
    /// 预付款金额
    var advanceTitle:UILabel!
    var advanceLabel:UILabel!
    /// 推广总金额
    var allPriceTitle:UILabel!
    var allPriceLabel:UILabel!
    var model:HWMineModel? {
        didSet {
            imageView.sd_setImage(with: URL(string: model?.head ?? ""), completed: nil)
            nameLabel.text = model?.username
            balanceLabel.text = model?.shareMoney
            advanceLabel.text = model?.yufu
            allPriceLabel.text = model?.money
        }
    }
    
    var withdrawalButtonClick:(()->())?
    var rechargeButtonClick:(()->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWMineTopCell {
    private func initUI() -> Void {
        
        imageView = UIImageView()
        imageView.backgroundColor = .clear
        imageView.layer.cornerRadius = 12*widthScale
        contentView.addSubview(imageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "名字", color: "#444444", font: 18*widthScale)
        contentView.addSubview(nameLabel)
        
        balanceTitle = HWLabel.createLabel(title: "可用余额 (元)", color: "#C1C1CB", font: 12*widthScale)
        contentView.addSubview(balanceTitle)
        
        balanceLabel = HWLabel.createBoldLabel(title: "", color: "#444444", font: 26*widthScale)
        contentView.addSubview(balanceLabel)
        
        withdrawalButton = UIButton(type: .custom)
        withdrawalButton.setTitle("提现", for: .normal)
        withdrawalButton.titleLabel?.font = .boldSystemFont(ofSize: 16*widthScale)
        withdrawalButton.setTitleColor(.colorWithHexString(hexString: "#5559EA"), for: .normal)
        withdrawalButton.addTarget(self, action: #selector(withdrawalButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(withdrawalButton)
        
        lineView = UIView()
        lineView.backgroundColor = .colorWithHexString(hexString: "#CCCCCC")
        contentView.addSubview(lineView)
        
        rechargeButton = UIButton(type: .custom)
        rechargeButton.setTitle("充值", for: .normal)
        rechargeButton.titleLabel?.font = .boldSystemFont(ofSize: 16*widthScale)
        rechargeButton.setTitleColor(.colorWithHexString(hexString: "#444444"), for: .normal)
        rechargeButton.addTarget(self, action: #selector(rechargeButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(rechargeButton)
        
        advanceTitle = HWLabel.createLabel(title: "预付款金额 (元)", color: "#C1C1CB", font: 12*widthScale)
        contentView.addSubview(advanceTitle)
        
        advanceLabel = HWLabel.createBoldLabel(title: "", color: "#444444", font: 16*widthScale)
        contentView.addSubview(advanceLabel)
        
        allPriceTitle = HWLabel.createLabel(title: "推广总金额 (元)", color: "#C1C1CB", font: 12*widthScale)
        contentView.addSubview(allPriceTitle)
        
        allPriceLabel = HWLabel.createBoldLabel(title: "", color: "#444444", font: 16*widthScale)
        contentView.addSubview(allPriceLabel)
    }
    
    private func initFrame() -> Void {
        imageView.snp.makeConstraints { make in
            make.top.equalTo(20*widthScale)
            make.left.equalTo(30*widthScale)
            make.width.height.equalTo(58*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(imageView.snp.right).offset(10*widthScale)
            make.centerY.equalTo(imageView)
        }
        
        balanceTitle.snp.makeConstraints { make in
            make.left.equalTo(36*widthScale)
            make.top.equalTo(imageView.snp.bottom).offset(20*widthScale)
        }
        
        balanceLabel.snp.makeConstraints { make in
            make.left.equalTo(balanceTitle)
            make.top.equalTo(balanceTitle.snp.bottom).offset(5*widthScale)
        }
        
        rechargeButton.snp.makeConstraints { make in
            make.right.equalTo(-39*widthScale)
            make.top.equalTo(116*widthScale)
        }
        
        lineView.snp.makeConstraints { make in
            make.width.equalTo(1)
            make.height.equalTo(14*widthScale)
            make.right.equalTo(rechargeButton.snp.left).offset(-16*widthScale)
            make.centerY.equalTo(rechargeButton)
        }
        
        withdrawalButton.snp.makeConstraints { make in
            make.right.equalTo(lineView.snp.left).offset(-17*widthScale)
            make.centerY.equalTo(rechargeButton)
        }
        
        advanceTitle.snp.makeConstraints { make in
            make.left.equalTo(balanceTitle)
            make.top.equalTo(balanceLabel.snp.bottom).offset(27*widthScale)
        }
        
        advanceLabel.snp.makeConstraints { make in
            make.left.equalTo(advanceTitle)
            make.top.equalTo(advanceTitle.snp.bottom).offset(7*widthScale)
        }
        
        allPriceTitle.snp.makeConstraints { make in
            make.right.equalTo(-41*widthScale)
            make.centerY.equalTo(advanceTitle)
        }
        
        allPriceLabel.snp.makeConstraints { make in
            make.centerY.equalTo(advanceLabel)
            make.left.equalTo(allPriceTitle)
        }
        
    }
}

extension HWMineTopCell {
    @objc func withdrawalButtonClick(_ button:UIButton) -> Void {
        self.withdrawalButtonClick?()
    }
    
    @objc func rechargeButtonClick(_ button:UIButton) -> Void {
        self.rechargeButtonClick?()
    }
}
